import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import 'TextUtils.dart';

class MyTextField extends StatelessWidget {
  final String? hintText;
  final TextEditingController controller;
  final TextStyle? hintStyle;
  final TextStyle? style;
  final int? maxLength;
  final Function()? onTap;
  final Function(String)? onChanged;
  final TextInputType? keyboardType;
  final EdgeInsets? contentPadding;
  final bool? clean;
  final bool? enable;
  final Widget? rightWidget;
  final MaxLengthEnforcement? maxLengthEnforcement;
  final List<TextInputFormatter>? inputFormatters;
  final ValueNotifier<bool> isShowPassword = ValueNotifier(false);
  final bool? isPassword;
  final FocusNode? focusNode;
  final int? maxLines;
  final TextInputAction? textInputAction;
  final ValueChanged<String>? onSubmitted;

  MyTextField({
    Key? key,
    required this.controller,
    this.hintText,
    this.hintStyle,
    this.style,
    this.onTap,
    this.keyboardType,
    this.inputFormatters,
    this.contentPadding,
    this.clean,
    this.isPassword,
    this.maxLength,
    this.rightWidget,
    this.maxLengthEnforcement,
    this.enable,
    this.focusNode,
    this.maxLines,
    this.onChanged,
    this.textInputAction,
    this.onSubmitted,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ValueListenableBuilder(
        valueListenable: controller,
        builder: (context, value, child) {
          return ValueListenableBuilder(
              valueListenable: isShowPassword,
              builder: (context, value, child) {
                return TextField(
                  onSubmitted: onSubmitted,
                  onChanged: onChanged,
                  maxLines: maxLines,
                  focusNode: focusNode,
                  maxLengthEnforcement:maxLengthEnforcement?? MaxLengthEnforcement.none,
                  enabled: enable,
                  controller: controller,
                  maxLength: maxLength,
                  textAlignVertical: TextAlignVertical.center,
                  style: style ??  TextStyle(color: Color(0xff333333), fontSize: 14),
                  // 设置输入框内文字的样式
                  keyboardType: keyboardType,
                  inputFormatters: inputFormatters,
                  textInputAction: textInputAction,
                  obscureText: (isPassword ?? false) ?!isShowPassword.value:false,
                  decoration: InputDecoration(
                      counterText: "", // 禁用底部计数字符提示
                      hintText: hintText,
                      hintStyle: hintStyle ??
                          TextStyle(color: Colors.grey, fontSize: 14),
                      isDense: true,
                      isCollapsed: true,
                      border: InputBorder.none,
                      focusedBorder: InputBorder.none,
                      contentPadding: contentPadding,
                      suffixIcon: ((clean ?? false) || (isPassword ?? false)) &&
                          TextUtils.isNotEmpty(controller.text)
                          ? InkWell(
                        onTap: onTap ??
                                () {
                              if (clean ?? false) {
                                controller.clear(); // 默认点击清空
                              }
                              if (isPassword ?? false) {
                                isShowPassword.value=!isShowPassword.value;
                              }
                            },
                        child: Padding(
                          padding: const EdgeInsets.only(right: 10),
                          child: (clean ?? false)
                              ? Icon(
                            Icons.clear,
                            color: Colors.grey,
                          )
                              : Icon(
                            isShowPassword.value
                                ? Icons.visibility
                                : Icons.visibility_off,
                            color: Colors.black,
                          ),
                        ),
                      ) : rightWidget,
                      suffixIconConstraints: BoxConstraints(
                        maxWidth: 25, // 设置最小高度来确保图标垂直居中
                        maxHeight: 16, // 设置最小宽度来确保图标不会被压缩
                      )),
                );
              });
        });
  }
}
